---@class cache 缓存
---@field data table<K, V>: { [K]: V } 数据
cache = class()

---@generic T
---@alias cache_call fun(K:T,V:T):void

function cache:ctor()
    self.data = {}
end

---@generic K
---@generic V
---设置值(无限时长)
---@param K K
---@param V V
function cache:set(K, V)
    self.data[K] = V
end

---@generic K
---@generic V
---设置值(缓存时间)
---@param K K
---@param V V
---@param time number 指定缓存时间
---@param call cache_call 到期回调
function cache:set_time(K, V, time, call)
    self.data[K] = V
    GT.timer.obj({
        count = 1,
        delay = time,
        immediately = false,
        callback = function(data, t)
            ---到期后执行回调
            if call~=nil then
                call(K, self.data[K])
            end
            

            self.data[K] = nil
        end
    })
end

---@generic V
---@generic K
---获取值
---@param K K
---@return V
function cache:get(K)
    return self.data[K]
end

---@generic K
---删除
---@param K K
function cache:del(K)
    self.data[K] = nil
end


